﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Psm.PubSub.Messaging;
using Psm.Container;
using System.Diagnostics;
using System.Reflection;

namespace PublisherSim
{
    class Program
    {
        static void Main(string[] args)
        {
            SetConsole();

            int MESSAGE_COUNT = 1000000;
            Repository.Initialize();
            Psm.PubSub.Client.PubSubClient client = new Psm.PubSub.Client.PubSubClient("UP_STREAM", null);
            Queue<DataMessage> messageQueue = new Queue<DataMessage>(MESSAGE_COUNT);
            Console.WriteLine("Prepareing Messages...");
            for (int index = 0; index < MESSAGE_COUNT; index++)
            {
                DataMessage message = client.CreateDataMessage("TOPIC1");
                message.AddData("00001", (index+1).ToString());
                message.AddData("00002", "00000002" );
                message.AddData("00003", "00000003");
                message.AddData("00004", "00000004");
                message.AddData("00005", "00000005");
                message.AddData("00006", "00000006");
                message.AddData("00007", "00000007");
                message.AddData("00008", "00000008");
                message.AddData("00009", "00000009");
                message.AddData("00010", "00000010");
                messageQueue.Enqueue(message);
            }
            Console.WriteLine("Prepareing Messages Done.");
            Stopwatch stopwatch = Stopwatch.StartNew();
            while (messageQueue.Count > 0)
                client.SendMessage(messageQueue.Dequeue());
            client.Flush();
            stopwatch.Stop();
            Console.WriteLine("{0} messages queued in {1} ms", MESSAGE_COUNT, stopwatch.ElapsedMilliseconds);
            Console.ReadKey();
            client.Close();
        }

        private static void SetConsole()
        {
            Console.SetBufferSize(500, 2000);
            Console.SetWindowPosition(0, 0);
            Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Title = Process.GetCurrentProcess().ProcessName;
        }
    }
}
